package com.batchengine.worker;

import com.batchengine.task.TaskContext;
import static com.batchengine.task.TaskUtil.getTaskContext;
import com.batchengine.util.LoggingFactory;
import static com.batchengine.worker.Status.FINISHED;
import static com.batchengine.worker.Status.RUNNING;
import org.apache.log4j.Logger;

import static java.text.MessageFormat.format;

/**
 * User: sunil.incubator@yahoo.com
 * Date: Sep 15, 2009
 * Time: 11:49:01 PM
 */
public class ScheduledDataGatherer<T> extends DataGatherer {

    private static final Logger logger = LoggingFactory.createLogger();

    public ScheduledDataGatherer(String name, long taskId, WorkSelector<T> workSelector) {
        super(name, taskId, workSelector);
    }

    @Override
    public void run() {

        try {
            setStatus(RUNNING);
            doWork(getTaskContext(getJobId()));
        } catch (Exception ignored) {
            logger.error(format("error running the ScheduledDataGatherer {0}", getName()), ignored);
        } finally {
            cleanUp();
            setStatus(FINISHED);
        }
    }

    protected void doWork(TaskContext context) {


        try {
            boolean filled = workSelector.fillWork();
        } catch (InterruptedException e) {
            logger.error("error@ScheduledDataGatherer#doWork", e);
        }
    }
}
